﻿using DataServer.CommonLib.Models.DataConfig;
using Newtonsoft.Json;
using System.Diagnostics.CodeAnalysis;

namespace DataServer.ApplicationLib.Controls
{
    public class ControlTextBox : ControlBase
    {
        public ControlTextBoxModel? Data { get; set; }

        public override void BuildEditDefineValue(ConfigFieldInfoResponseDto configFieldInfoDto)
        {
            if (Data != null)
            {
                configFieldInfoDto.MaxLengthTextBox = Data.MaxLength;
                configFieldInfoDto.RegexExpTextBox = Data.RegexExp;
                configFieldInfoDto.RegexExpDescTextBox = Data.RegexExpDesc;
            }
        }

        public override ControlBase Deserialize(string controlContent)
        {
            if (!string.IsNullOrEmpty(controlContent))
                Data = JsonConvert.DeserializeObject<ControlTextBoxModel>(controlContent);
            return this;
        }

        public override string Serialize()
        {
            if (Data != null)
            {
                return JsonConvert.SerializeObject(Data);
            }
            return string.Empty;
        }

        public override Tuple<bool, string, ControlBase> TryValidationCreatorControl(ConfigFieldEditRequestDto model)
        {
            var textBoxModel = new ControlTextBoxModel
            {
                ControlName = model.ControlType,
                MaxLength = model.MaxLengthTextBox,
                RegexExp = model.RegexExpTextBox,
                RegexExpDesc = model.RegexExpTextBox,
                TargetFieldColumn = model.FieldCode
            };

            var controlBase = new ControlTextBox
            {
                Data = textBoxModel
            };

            return new Tuple<bool, string, ControlBase>(true, string.Empty, controlBase);
        }
    }

    public class ControlTextBoxModel
    {
        [NotNull]
        public string? ControlName { get; set; }

        public int? MaxLength { get; set; }

        public string? RegexExp { get; set; }

        [NotNull]
        public string? TargetFieldColumn { get; set; }

        public string? RegexExpDesc { get; set; }

    }
}
